package com.sandama.bbs.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.facebook.drawee.view.SimpleDraweeView;
import com.sandama.bbs.R;
import com.sandama.bbs.model.Game;
import org.xml.sax.XMLReader;

import java.util.List;

/**
 * Author:TongxiJi
 * Function:
 * Create Date:十月09,2015
 */
public class GroupGamesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Game> gameList;

    public void setGroupList(List<Game> gameList) {
        this.gameList = gameList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_group_game_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        Game game = gameList.get(position);
        if (game == null) return;
        viewHolder.ivGameLogo.setImageURI(Uri.parse(game.getLogoUrl()));
        viewHolder.tvGameName.setText(game.getName());
        viewHolder.tvGameHotTopics.setText(Html.fromHtml(game.getHotTopics()));
        viewHolder.tvGameHotTopics.setMovementMethod(LinkMovementMethod.getInstance());
        viewHolder.tvPostCount.setText("回帖"+game.getPostCount());
    }

    @Override
    public int getItemCount() {
        return gameList == null ? 0 : gameList.size();
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_group_game_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_game_logo)
        SimpleDraweeView ivGameLogo;
        @Bind(R.id.tv_game_name)
        TextView tvGameName;
        @Bind(R.id.tv_game_hot_topics)
        TextView tvGameHotTopics;
        @Bind(R.id.tv_post_count)
        TextView tvPostCount;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
